package hyman;
import jakarta.xml.bind.JAXBException;
import org.jpmml.evaluator.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class TestIt {
    public static void main(String[] args) throws JAXBException, IOException, ParserConfigurationException, SAXException {
        File pmmlFile = new File("LightGBMAudit.pmml");
//
//        Evaluator result = new LoadingModelEvaluatorBuilder()
//                // Ignore SAX Locator information to reduce memory consumption
//                .setLocatable(false)
//                // Pre-parse and intern PMML markup to improve performance and reduce memory consumption
//                .setVisitors(new DefaultVisitorBattery())
//                .load(pmmlFile)
//                .build();


        // Building a model evaluator from a PMML file
        try (InputStream is = TestIt.class.getClassLoader().getResourceAsStream("LightGBMAudit.pmml")) {
            if (is == null) {
                throw new FileNotFoundException("Resource LightGBMAudit.pmml not found");
            }

            Evaluator evaluator = new LoadingModelEvaluatorBuilder()
                    .load(is)
                    .build();

            evaluator.verify();
            // Printing input (x1, x2, .., xn) fields
            List<InputField> inputFields = evaluator.getInputFields();
            System.out.println("Input fields: " + inputFields);

            List<TargetField> targetFields = evaluator.getTargetFields();
            System.out.println("Target field(s): " + targetFields);

            List<OutputField> outputFields = evaluator.getOutputFields();
            System.out.println("Output fields: " + outputFields);

            // 构造并评估一条记录
            Map<String, Object> inputRecord = createRecord(
                    "NA", "Private", "NA", "Married", "Professional", "NA"

                    , "NA", 40
            );

            Map<String, ?> results = evaluator.evaluate(inputRecord);
            results = EvaluatorUtil.decodeAll(results);

            System.out.println("Input: " + inputRecord);
            System.out.println("Prediction Results: " + results);

            evaluator = null;

        }


    }

    static Map<String, Object> createRecord(Object age, String employment, String education, String marital,
                                     String occupation, Object income, String gender,
                                      Object hours) {
        //字段区分大小写
        Map<String, Object> record = new LinkedHashMap<>();
        record.put("Age", 38);
        record.put("Employment", "Private");
        record.put("Education", "College");
        record.put("Marital", "Married");
        record.put("Occupation", "Farming");
        record.put("Income", "148865.82");
//        record.put("Gender", "Male");
        record.put("Hours", 5);
        return record;
    }
}
